#!/bin/sh

echo '
  这个脚本将帮助你创建出来一个使用spring mvc、mybatis、mysql、velocity的Java应用程序。当然，并不会创建任何可用代码，只是为了免去每次创建无聊的目录结构。
'

mkdir -p src/{main,test}/{java,resources,webapp}
mkdir -p src/main/webapp/WEB-INF

echo -n "请输入Web应用名字："
read NAME

echo '<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>'$NAME'</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>' > src/main/webapp/WEB-INF/web.xml
echo 'web.xml创建成功'

echo '<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
      <!--mysql链接参数-->
      <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis?useUnicode=
true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"></property>
      <property name="username" value="root"></property>
      <property name="password" value="root"></property>
    </bean>

    <!--事务相关配置-->
    <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource" /> 
    </bean>

    <!--mybatis配置文件的路径-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="configLocation" value="classpath:/WEB-INF/MyBatis-Configuration.xml"></property>
      <property name="dataSource" ref="dataSource" />
    </bean>

    <!--mybatis和DAO/DAPImp的映射
    <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="mapperInterface" value="com.icreate.dao.UserDao"></property>
      <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean> 

    <bean id="userService" class="com.icreate.service.impl.UserServiceImpl">
      <property name="userDao" ref="userDao"></property>
    </bean>
    -->

    <!--velocity模板文件目录配置-->
    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
      <property name="resourceLoaderPath">
        <value>/WEB-INF/velocity/</value>
      </property>
      <property name="velocityProperties">
        <!--配置循环索引从0开始-->
        <props>
          <prop key="directive.foreach.counter.name">loopCounter</prop>
          <prop key="directive.foreach.counter.initial.value">0</prop>
        </props>
      </property>
    </bean>
    <bean id="viewResolver" class="org.springframework.
              web.servlet.view.velocity.VelocityViewResolver">
        <!--配置velocity文件的后缀名-->
        <property name="suffix"><value>.vm</value></property>
        <!--日期字符串输出格式配置-->
        <property name="dateToolAttribute">
          <value>dateTool</value>
        </property>
        <!--数字输出格式配置-->
        <property name="numberToolAttribute">
          <value>numberTool</value>
        </property>
        <!--将请求参数和session内容自动添加到ModelAndView中-->
        <property name="exposeRequestAttributes">
          <value>true</value>
        </property>
        <property name="exposeSessionAttributes">
          <value>true</value>
        </property>
    </bean>

    <!--Controller 配置参数-->
    <context:component-scan base-package="这里填写controller包的名字" />

    <!--view相关参数-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
          <!--不要放到WEB-INF的根目录下面，否则可以直接访问到-->
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <!--老的controller配置方式-->
    <bean name="/old" class="leolord.bookthief.OldController"/>
    <bean name="/old.htm" class="leolord.bookthief.OldController"/>

</beans>
'  > src/main/webapp/WEB-INF/applicationContext.xml
echo 'spring配置文件创建成功'

echo '<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
      <!--mapper文件配置
      <mapper resource="com/icreate/dao/UserDao.xml"/>
      -->
    </mappers>
</configuration>
' > src/main/webapp/WEB-INF/MyBatis-Configuration.xml
echo 'MyBatis配置文件创建成功'

echo '<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.icreate.dao.UserDao">
    <!-- 查询表中记录总数 -->
    <select id="countAll" resultType="int">
        select count(*) c from tb_user;
    </select>
    <!-- 查询表中的所有用户 -->
    <select id="selectAll" resultType="com.icreate.entity.User">
        select * from tb_user order by username asc
    </select>
    <!-- 向数据库中插入用户 -->
    <insert id="insert" parameterType="com.icreate.entity.User">
        insert into tb_user(username,password,email,sex,age) values(#{username},#{password},#{email},#{sex},#{age})
    </insert>
    <!-- 更新库中的用户 -->
    <update id="update" parameterType="com.icreate.entity.User">
        update tb_user set username=#{username},password=#{password},email=#{email},sex=#{sex},age=#{age} where username=#{username}
    </update>
    <!-- 删除用户 -->
    <delete id="delete" parameterType="String">
        delete from tb_user where username=#{username}
    </delete>
    <!-- 根据用户名查找用户 -->
    <select id="findByUserName" parameterType="String" resultType="com.icreate.entity.User">
        select * from tb_user where username=#{username}
    </select>
</mapper>
' > src/main/webapp/WEB-INF/mapper-sample.xml
echo 'mapper示例文件创建成功:src/main/webapp/WEB-INF/mapper-sample.xml'


echo "
应用程序${NAME}已经创建，可以使用的命令包括：
gradle   compileJava   #编译工程
gradle   war           #打包应用
gradle   idea          #将gradle工程转换为idea工程
gradle   clean         #清理工程
gradle   cleanIdea     #清理idea相关文件
gradle   check         #运行所有测试用例
gradle   test          #运行单元测试
gradle   appRun        #在jetty中运行应用
gradle   tasks         #查看更多可用命令

在此之前，你要创建至少一个Controller，并响应的修改/src/main/webapp/WEB-INF/ContextConfigLocation.xml中的内容
"
